package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.BusinessDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BusinessDaoImpl implements BusinessDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<Business> listBusinessByOrderTypeId(Integer orderTypeId) throws Exception {
        List<Business> list = new ArrayList<>();
        String sql = "select * from business where orderTypeId=? order by businessId";
        try {
            //获取连接
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,orderTypeId);
            rs = pst.executeQuery();
            while(rs.next()) {
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                list.add(business);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }
    /**
     * 根据商家编号查询商家信息
     * @param businessId
     * @return
     * @throws Exception
     */
    @Override
    public Business getBusinessById(Integer businessId) throws Exception {
        String sql ="select * from business where businessId=?";
        Business business = null;
        try {
            //获取数据库链接
            con = DBUtil.getConnection();
            //通过调用Connection接口中的方法prepareStatement(sql);获取PreparedStatement对象
            pst = con.prepareStatement(sql);
            //调用PreparedStatement对象中的set方法将？替换为具体的值
            pst.setInt(1, businessId);
            //执行查询
            rs = pst.executeQuery();
            //获取结果集的数据 next()方法是将光标向下移动一行,获取当前行的数据
            while(rs.next()) {
                business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
            }
        }finally {                   //这里不能处理异常，也就是没有catch，只有finally
            DBUtil.close(rs, pst);   //这里负责关闭PreparedStatement和ResultSet结果集
        }
        return business;
    }
}
